package com.semanticweb.webapp.model.ontology;

import org.apache.lucene.document.Document;

import com.semanticweb.webapp.ontology.CIDOC;
import com.semanticweb.webapp.ontology.Ont;
import com.semanticweb.webapp.ontology.Query;
import com.semanticweb.webapp.search.IDocument;
import com.semanticweb.webapp.search.SearchMD;

/**
 * Class which represents 'Datum nastanka' from the chart.
 * 
 * NOTE: this class represents time span, so on UI there should be two dates converted to one string value.
 * 
 * @author Lab
 * 
 */
public class CreationDate extends DefaultAbstractEntity<String> implements IDocument {

    public static final String ONT_PRODUCTION = "production";
    public static final String ONT_TIME_SPAN = "time-span";
    public static final String ONT_DATE = "date";

    public CreationDate(final String id) {
        super(id);
    }

    @Override
    public void toRDF() {
        if (this.value == null) {
            return;
        }

        final Query query = new Query();
        final String[] result = query.select("production").type("date", CIDOC.E50.queryValue())
                .label("date", this.value).type("timeSpan", CIDOC.E52.queryValue())
                .type("production", CIDOC.E12.queryValue()).connect("timeSpan", CIDOC.P78.queryValue(), "date")
                .connect("production", CIDOC.P4.queryValue(), "timeSpan").find();

        final String itemURI = Item.formatItemURI(this.id);
        String productionURI;
        if (result == null || result.length == 0) {
            productionURI = Ont.formatURI(this.id, ONT_PRODUCTION, ONT_DATE);
            final String dateURI = Ont.formatURI(this.id, ONT_DATE);
            final String timeSpanURI = Ont.formatURI(this.id, ONT_TIME_SPAN);

            Ont.getOntology().createIndividual(CIDOC.E50.getFullValue(), dateURI);
            Ont.getOntology().addLabel(dateURI, this.value);
            Ont.getOntology().createIndividual(CIDOC.E52.getFullValue(), timeSpanURI);
            Ont.getOntology().createIndividual(CIDOC.E12.getFullValue(), productionURI);

            Ont.getOntology().connectIndividuals(timeSpanURI, CIDOC.P78.getFullValue(), dateURI);
            Ont.getOntology().connectIndividuals(productionURI, CIDOC.P4.getFullValue(), timeSpanURI);
        } else {
            productionURI = result[0];
        }

        // connect to item
        Ont.getOntology().connectIndividuals(productionURI, CIDOC.P108.getFullValue(), itemURI);
    }

    public void fromRDF() {
        final String itemQueryURI = Item.formatItemQueryURI(this.id);
        final Query q = new Query();
        final String[] result = q.selectLabel().connect("production", CIDOC.P108.queryValue(), itemQueryURI)
                .type("production", CIDOC.E12.queryValue()).type("timeSpan", CIDOC.E52.queryValue())
                .connect("production", CIDOC.P4.queryValue(), "timeSpan").type("date", CIDOC.E50.queryValue())
                .connect("timeSpan", CIDOC.P78.queryValue(), "date").findLabel("date");
        if (result != null && result.length > 0) {
            this.value = result[0];
        }
    }

    @Override
    public void toIndexDocument(final Document doc) {
        if (this.value != null) {
            doc.add(SearchMD.CONTENT.createField(this.value));
        }
    }

}
